import type { Directive, DirectiveBinding } from "vue";
import { ElMessage } from "element-plus";

/**
 *
 * @param el
 * @param binding
 * v-input="value" 获取value内的值并复制
 */
const vCopy: Directive = (el: HTMLElement, binding: DirectiveBinding) => {
    let inp: HTMLInputElement;
    inp = el as HTMLInputElement;
    
    let copyData: string | number;//复制值
    copyData = binding.value;

    function handleClick() {
        const input = document.createElement("input");
        input.style.opacity = '0'
        input.value = copyData.toLocaleString();
        document.body.appendChild(input);
        input.select();
        document.execCommand("Copy");
        document.body.removeChild(input);
        ElMessage({
            type: "success",
            message: "复制成功"
        });
    }
    inp.addEventListener('click', () => {
        handleClick();
    });
}

export default vCopy;